//https://leetcode.cn/problems/valid-anagram/
package codeRandomThoughts.Test242有效的字母异位词;

import java.util.HashMap;

public class Solution {
    /**
     * 熟悉了一下HashMap的用法,其实这道题只需要自己建立一个哈希表就行
     * 长度对应26个字母就好了
     *
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length())
            return false;
        HashMap<String, Integer> hashMapS = new HashMap<>();
        HashMap<String, Integer> hashMapT = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            String c = String.valueOf(s.charAt(i));
            hashMapS.put(c, hashMapS.getOrDefault(c, 0) + 1);
        }
        for (int i = 0; i < t.length(); i++) {
            String c = String.valueOf(t.charAt(i));
            hashMapT.put(c, hashMapT.getOrDefault(c, 0) + 1);
        }
        for (String c : hashMapS.keySet()) {
            if (hashMapS.get(c).equals(hashMapT.get(c))) ;
            return false;
        }
        return true;
    }

    public boolean isAnagram2(String s, String t) {

        char[] cs = s.toCharArray();
        char[] ct = t.toCharArray();
        int[] map = new int[26];

        for (char c : cs) {
            map[c - 'a']++;
        }

        for (char c : ct) {
            map[c - 'a']--;
        }

        for (int i : map) {
            if (i != 0) return false;
        }

        return true;
    }
}
